package com.gw.quotes.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

import com.gw.quotes.AppInfo;
import com.gw.quotes.R;
import com.gw.quotes.adapter.EvaluationAdapter;
import com.gw.quotes.entity.business.SearchEstimateEntity;
import com.gw.quotes.entity.business.SearchEstimateEntity.Ordersrecord;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.SearchEstimateServiceImpl;
import com.gw.quotes.util.CommonTools;
import com.yxw.view.YXWXListView;
import com.yxw.view.YXWXListView.IXListViewListener;

public class MineEvaluationActivity extends BaseActivity implements
		IXListViewListener {

	/**
	 * 当前页
	 */
	private int pageno = 1;
	/**
	 * 一页数量
	 */
	private static int pagesize = 10;
	/**
	 * 查询的公司名称 ""表示不限制
	 */
	private String buyerorgname = "";
	/**
	 * (null全部,1已评价,0待评价) 默认1
	 */
	private String estamatestate = "";

	private EvaluationAdapter evaAdapter;
	private ArrayList<Ordersrecord> list;

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	@ViewInject(id = R.id.eva_listView)
	private YXWXListView eva_listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mine_evaluation);
		init(YXWXListView.ACTION_REFRESH);
		eva_listView.startRefresh();
	}

	private void init(int requestType) {
		if (requestType == YXWXListView.ACTION_REFRESH || list == null) {
			list = (ArrayList<Ordersrecord>) aCache
					.getAsObject("Ordersrecords_"
							+ getLoginUser().getUsername());
			evaAdapter = new EvaluationAdapter(this, R.layout.item_evaluation,
					list);

			eva_listView.setPullLoadEnable(true);
			eva_listView.setXListViewListener(this);
			eva_listView.setAdapter(evaAdapter);
			eva_listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (list.get((int) id).getEstimatestate() == 1) {
						// 已评价，进入详细页面
						Intent intent = new Intent(MineEvaluationActivity.this,
								MineEvaluationDetailActivity.class);
						intent.putExtra("Contractguid", list.get((int) id)
								.getContractguid());
						intent.putExtra("Ordercode", list.get((int) id)
								.getOrdercode());
						startActivity(intent);
					} else {
						CommonTools.showShortToast(MineEvaluationActivity.this,
								"该订单尚未评价");
					}

				}
			});
		} else {
			evaAdapter.notifyDataSetChanged();
		}
	}

	private void requestList(final int requestType) {
		// 设置参数
		String jsonconditions = "{'pageno':'" + pageno + "','pagesize':'"
				+ pagesize + "','buyerorgname':'" + buyerorgname
				+ "','estamatestate':'" + estamatestate + "'}";
		BaseServiceInter httpService = new SearchEstimateServiceImpl();
		Request request = new Request();
		request.setLoginname(getLoginUser().getLoginname());
		request.setTicket(getLoginUser().getTicket());
		request.setJsonconditions(jsonconditions);

		Response response = httpService.handler(request,
				new HttpCallBack<SearchEstimateEntity>() {
					@Override
					public void onFinished(SearchEstimateEntity response) {
						if (response != null
								&& response.getResult() == Response.RESULT_SUCCESS) {

							if (requestType == YXWXListView.ACTION_REFRESH) {
								// 刷新或者第一次加载时的操作
								list = response.getOrdersrecords();
								if (list != null) {
									aCache.put("Ordersrecords_"
											+ getLoginUser().getUsername(),
											response.getOrdersrecords());
								}

								if (list.size() < 10) {
									eva_listView.setPullLoadEnable(false);
								} else {
									eva_listView.setPullLoadEnable(true);
								}

							} else {
								// 上拉加载更多
								if (response.getOrdersrecords().size() < 10) {
									eva_listView.setPullLoadEnable(false);
									CommonTools.showShortToast(
											MineEvaluationActivity.this,
											"已是最后一页！");
								}
								CommonTools.MergeArrayList(list,
										response.getOrdersrecords());
							}
							init(requestType);
						}
						onLoad();
					}
				});
	}

	/**
	 * onClick 返回按钮
	 * 
	 * @param v
	 */
	public void back(View v) {
		finish();
	}

	/**
	 * 结束刷新和加载
	 */
	private void onLoad() {
		eva_listView.stopRefresh();
		eva_listView.stopLoadMore();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		eva_listView.setRefreshTime(df.format(new Date()));
	}

	@Override
	public void onRefresh() {
		pageno = 1;
		requestList(YXWXListView.ACTION_REFRESH);
	}

	@Override
	public void onLoadMore() {
		pageno++;
		requestList(YXWXListView.ACTION_LOADMORE);

	}

}
